home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_oth / forchek1 / symtab.h < prev    next >
C/C++ Source or Header  |  1991-11-05  |  10KB  |  407 lines

  1. /* symtab.h:
  2.  
  3.  Shared declarations for symbol-table routines.  Note: uses
  4.  declarations in defs.h.
  5.  
  6.     Copyright (C) 1991 by Robert K. Moniot.
  7.     This program is free software.  Permission is granted to
  8.     modify it and/or redistribute it, retaining this notice.
  9.     No guarantees accompany this software.
  10.  
  11.  
  12. */
  13.  
  14. #ifdef SYMTAB   /* "home" for variables is symtab.c */
  15. #define SYM_SHARED
  16. #else
  17. #define SYM_SHARED extern
  18. #endif
  19.  
  20.  
  21.   /* Definitions of symbol table information */
  22.  
  23. /* Token subclasses (classes are in tokdefs.h)
  24.  */
  25.  
  26. #define relop_EQ 0
  27. #define relop_NE 1
  28. #define relop_LE 2
  29. #define relop_LT 3
  30. #define relop_GE 4
  31. #define relop_GT 5
  32.  
  33.  
  34.  
  35.  /* Storage Class types for variables, consts, and externals */
  36. #define class_VAR 0
  37. #define class_SUBPROGRAM 1
  38. #define class_COMMON_BLOCK 2
  39. #define class_STMT_FUNCTION 3
  40. #define class_LABEL 4
  41.  
  42.  
  43.  /* Data types for variables, consts, and externals */
  44.  /* N.B. 0 thru 7 are wired into lookup tables in exprtype.c */
  45. #define type_UNDECL 0
  46. #define type_INTEGER 1
  47. #define type_REAL 2
  48. #define type_DP 3
  49. #define type_COMPLEX 4
  50. #define type_LOGICAL 5
  51. #define type_STRING 6
  52. #define type_HOLLERITH 7
  53. #define type_GENERIC 8
  54. #define type_SUBROUTINE 9
  55. #define type_COMMON_BLOCK 10
  56. #define type_PROGRAM 11
  57. #define type_BLOCK_DATA 12
  58. #define type_LABEL 13
  59.  
  60.     /* test for types usable in exprs */
  61. #define is_computational_type(t) ((t) <= 7)
  62.     /* test for arith, char, or logical type */
  63. #define is_const_type(t) ((datatype_of(t) > 0) && (datatype_of(t) <= 6))
  64.  
  65.  
  66. typedef unsigned char BYTE;
  67.  
  68.  
  69.   /* Array of class and type name translations */
  70. #ifdef SYMTAB
  71. char *class_name[] = {
  72.  "",
  73.  "subprog",
  74.  "common",
  75.  "stmt fun",
  76.  "label",
  77. };
  78. char *type_name[] = {
  79.  "undf",
  80.  "intg",
  81.  "real",
  82.  "dble",
  83.  "cplx",
  84.  "logl",
  85.  "char",
  86.  "holl",
  87.  "genr",
  88.  "subr",
  89.  "comm",
  90.  "prog",
  91.  "data",
  92.  "labl",
  93. };
  94.  
  95.   /* Typical sizes of objects of each data type */
  96. BYTE type_size[]={  /* for use in check_mixed_common */
  97.  0, /*undf*/
  98.  4, /*intg*/
  99.  4, /*real*/
  100.  8, /*dble*/
  101.  8, /*cplx*/
  102.  4, /*logl*/
  103.  1, /*char*/
  104.  4, /*holl*/
  105.  0, /*genr*/
  106.  0, /*subr*/
  107.  0, /*comm*/
  108.  0, /*prog*/
  109.  0, /*data*/
  110.  0, /*labl*/
  111. };
  112.  
  113. #else
  114. extern char *class_name[];
  115. extern char *type_name[];
  116. extern BYTE type_size[];
  117. #endif
  118.  
  119.  
  120.   /* implicit and default typing lookup table */
  121. SYM_SHARED
  122. int implicit_type[26]; /* indexed by [char - 'A'] */
  123.  
  124.  
  125.  
  126.  /* Declaration of Token data structure.  N.B. do not change without
  127.     consulting preamble of fortran.y for uses with nonterminals.
  128.   */
  129. struct tokstruct {
  130.  union {
  131.   int integer;
  132.   double dbl;
  133.   char *string;
  134.  } value;
  135.  struct tokstruct *next_token;
  136.  short class,subclass;
  137.  unsigned line_num; /* Line and column where token occurred */
  138.  unsigned col_num : 8;
  139. };
  140.  
  141. typedef struct tokstruct Token;
  142.  
  143. #define YYSTYPE Token /* Type defn for yylval and Yacc stack */
  144.  
  145.  
  146.  
  147. SYM_SHARED
  148. unsigned long loc_symtab_top, /* Next avail spot in local symbol table */
  149.    glob_symtab_top;  /* Ditto global */
  150.  
  151. SYM_SHARED
  152. char strspace[STRSPACESZ]; /* String space for storing identifiers */
  153.  /* Stringspace is partitioned into local (growing from bottom up)
  154.     and global (growing from top down). */
  155.  
  156. SYM_SHARED
  157. unsigned long loc_str_top, /* Top of local stringspace */
  158.    glob_str_bot;  /* Bottom of global stringspace */
  159.  
  160. SYM_SHARED
  161.   unsigned long token_space_top; /* Top of token space */
  162. SYM_SHARED
  163.   Token tokenspace[TOKENSPACESZ];
  164.  
  165.  
  166.   /* Define names for anonymous things */
  167. #ifdef SYMTAB
  168. char *blank_com_name = "%BLANK",  /* id for blank common entry in symtab */
  169.      *unnamed_prog="%MAIN";   /* id for unnamed program module */
  170. #else
  171. extern char *blank_com_name,
  172.      *unnamed_prog;
  173. #endif
  174.  
  175.                 /* Symbol table argument list declarations */
  176.  
  177. typedef union {  /* InfoUnion: misc info about symtab entry */
  178.       unsigned long array_dim; /* array size and no. of dims */
  179.       struct ALHead *arglist; /* ptr to func/subr argument list */
  180.       struct CMHead *comlist;    /* ptr to common block list */
  181.       struct TLHead *toklist;  /* ptr to token list */
  182.       struct IInfo *intrins_info;/* ptr to intrinsic func info */
  183.       int int_value;  /* value of integer parameter */
  184. } InfoUnion;
  185.  
  186. typedef struct { /* ArgListElement: holds subprog argument data */
  187.  InfoUnion info;
  188.  BYTE type;
  189.  unsigned is_lvalue: 1,
  190.    set_flag: 1,
  191.    assigned_flag: 1,
  192.    used_before_set: 1,
  193.    array_var: 1,
  194.    array_element: 1,
  195.    declared_external: 1;
  196. } ArgListElement;
  197.  
  198.  
  199. typedef struct ALHead {     /* ArgListHeader: head node of argument list */
  200.  BYTE type;
  201.  short numargs;
  202.  ArgListElement *arg_array;
  203.  struct SymtEntry *module;
  204.  char *filename,*topfile;
  205.  unsigned
  206.       line_num,
  207.       is_defn: 1,
  208.       is_call: 1,
  209.       external_decl: 1, /* EXTERNAL decl, not arg list */
  210.              actual_arg: 1; /* subprog passed as arg */
  211.  struct ALHead *next;
  212. } ArgListHeader;
  213.  
  214.   /* Symbol table common block list declarations */
  215.  
  216. typedef struct { /* ComListElement: holds common var data */
  217.  unsigned long dimen_info;
  218.  BYTE type;
  219. } ComListElement;
  220.  
  221. typedef struct CMHead { /* ComListHeader: head node of common var list */
  222.  short numargs;
  223.  short flags;
  224.  unsigned line_num;
  225.  ComListElement *com_list_array;
  226.  struct SymtEntry *module;
  227.  char *filename,*topfile;
  228.  struct CMHead *next;
  229. } ComListHeader;
  230.  
  231.  
  232. typedef struct TLHead { /* TokenListHeader: head node of token list */
  233.  Token *tokenlist;
  234.  struct TLHead *next;
  235.  char *filename;
  236.  unsigned line_num;
  237.  unsigned
  238.    external_decl:1,
  239.    actual_arg:1;
  240. } TokenListHeader;
  241.  
  242.  
  243.    /* Structure for intrinsic-function info */
  244. typedef struct IInfo{
  245.  char *name;
  246.  short num_args,arg_type,result_type;
  247. } IntrinsInfo;
  248.  
  249.  
  250.  
  251.  
  252.   /*  Identifier symbol table declaration */
  253.  
  254.  
  255. typedef struct SymtEntry{
  256.  char *name;             /* Identifier name in stringspace */
  257.  InfoUnion info;
  258.  struct SymtEntry *equiv_link; /* Link for equivalence lists */
  259.  BYTE  type;  /* Type & storage class: see macros below */
  260.        /* Flags: if changed, update macro clear_symtab_flags below */
  261.  unsigned
  262.       used_flag: 1,
  263.       set_flag: 1,
  264.       assigned_flag: 1,
  265.       used_before_set: 1,
  266.       is_current_module: 1,
  267.       library_module: 1,
  268.       array_var: 1,
  269.       common_var: 1,
  270.       entry_point: 1,
  271.       parameter: 1,
  272.       argument: 1,
  273.       external: 1,
  274.       intrinsic: 1,
  275.       invoked_as_func: 1,
  276.       defined_in_include: 1,
  277.       declared_external: 1;
  278. } symtab;
  279.  
  280.  
  281.  
  282.   /* Macro to clear all flags in symbol table entry */
  283.  
  284. #define clear_symtab_flags(S) ((S)->used_flag=  (S)->set_flag= \
  285.  (S)->assigned_flag= (S)->used_before_set=  (S)->is_current_module= \
  286.  (S)->library_module= \
  287.  (S)->array_var= (S)->common_var=  (S)->entry_point=  (S)->parameter= \
  288.  (S)->argument=  (S)->external=  (S)->intrinsic= \
  289.  (S)->invoked_as_func= (S)->defined_in_include= \
  290.  (S)->declared_external= \
  291.   0)
  292.  
  293.  
  294.  /* These macros pack and unpack datatype and storage class in type
  295.     field of symbol table entry. Datatype is least 4 bits. */
  296.  
  297. #define datatype_of(TYPE) ((TYPE) & 0xF)
  298. #define storage_class_of(TYPE) ((TYPE) >> 4)
  299. #define type_byte(SCLASS,DTYPE) (((SCLASS)<<4) + (DTYPE))
  300.  
  301.  
  302.  /* This macro is for pattern matching in flag checking */
  303.  
  304. #define flag_combo(A,B,C) (((A)<<2) | ((B)<<1) | (C))
  305.  
  306.  
  307.  /* These macros are for dimensions & sizes of arrays */
  308.  
  309. #define array_dims(dim_info) ((dim_info)&0xF)
  310. #define array_size(dim_info) ((dim_info)>>4)
  311. #define array_dim_info(dim,size) (((long)(size)<<4)+(dim))
  312.  
  313.  
  314.  
  315.   /* Defns used by expression type propagation mechanisms
  316.      in fortran.y and exprtype.c  The flags go in token.subclass
  317.    */
  318.  
  319. #define make_true(flag,x) ((x) |= (flag))  /* x.flag <-- true   */
  320. #define make_false(flag,x) ((x) &= ~(flag))  /* x.flag <-- false  */
  321. #define is_true(flag,x) ((x) & (flag))   /* x.flag == true?   */
  322. #define copy_flag(flag,x,y)  ((x) |= ((y)&(flag))) /* x.flag <-- y.flag */
  323.  
  324. #define ID_EXPR 0x1
  325. #define LVALUE_EXPR 0x2
  326. #define CONST_EXPR 0x4
  327. #define NUM_CONST 0x8
  328. #define ARRAY_ID_EXPR 0x10
  329. #define INT_QUOTIENT_EXPR 0x20
  330. #define STMT_FUNCTION_EXPR 0x40
  331. #define SET_FLAG 0x80  /* these are for id's and lvalues */
  332. #define ASSIGNED_FLAG 0x100
  333. #define USED_BEFORE_SET 0x200
  334. #define COMMA_FLAG  0x400        /* keeps track of extra or missing commas
  335.        in exprlists */
  336.  
  337.  
  338. SYM_SHARED
  339. symtab glob_symtab[GLOBSYMTABSZ],
  340.  loc_symtab[LOCSYMTABSZ];
  341.  
  342.   /*  Identifier hashtable declaration  */
  343.  
  344. SYM_SHARED
  345. struct {
  346.  char *name;  /* Identifier name in stringspace */
  347.  symtab *loc_symtab, /* Local symtab entry for vars etc. */
  348.   *glob_symtab, /* Global symtab entry for vars etc. */
  349.   *com_loc_symtab,/* Local symtab entry for common blocks */
  350.   *com_glob_symtab;/* Global ditto */
  351. } hashtab[HASHSZ];
  352.  
  353.  
  354.  
  355.   /* Shared routines */
  356.  
  357.    /* in fortran.y/fortran.c */
  358. void
  359. check_seq_header();
  360.  
  361.    /* in prsymtab.c */
  362. void
  363. debug_symtabs(), print_loc_symbols();
  364.  
  365.    /* in symtab.c */
  366. void
  367. call_func(), call_subr(), declare_type(), def_arg_name(),
  368. def_array_dim(), def_com_block(), def_com_variable(),
  369. def_equiv_name(), def_ext_name(), def_function(), def_intrins_name(),
  370. def_parameter(),
  371. def_stmt_function(), do_ASSIGN(), do_assigned_GOTO(), do_ENTRY(),
  372. do_RETURN(), equivalence(),
  373. init_globals(), init_symtab(),
  374. process_lists(), ref_array(), ref_variable(),
  375. set_implicit_type(),
  376. stmt_function_stmt(),use_actual_arg(), use_implied_do_index(),
  377. use_io_keyword(),
  378. use_lvalue(), use_parameter(),
  379. use_var_as_subscr(), use_variable();
  380.  
  381. Token
  382. *new_token();
  383.  
  384. symtab
  385.  *install_local(), *install_global();
  386.  
  387. unsigned
  388. hash_lookup();
  389.  
  390. int
  391. def_curr_module(), get_type(), int_expr_value();
  392.  
  393. char *
  394. token_name();
  395.     /* in hash.c (now symtab.c) */
  396. unsigned long
  397. hash(), kwd_hash(), rehash();
  398.  
  399.    /* in symtab2.c */
  400. void    /* exprtype routines */
  401. binexpr_type(),unexpr_type(),assignment_stmt_type(),
  402. func_ref_expr(),primary_id_expr();
  403.  
  404.  
  405.  
  406.  
  407.